import json
import os.path

from logger import _logging

logger = _logging(filename="./logs/address.log")
root_dir = os.path.dirname(os.path.abspath(__file__))
lib_dir = os.path.join(root_dir, "lib")

address_file_path = os.path.join(lib_dir, "address.json")
province_file_path = os.path.join(lib_dir, "province.json")  # 省份
city_file_path = os.path.join(lib_dir, "city.json")  # 城市
district_file_path = os.path.join(lib_dir, "district.json")  # 区县
town_file_path = os.path.join(lib_dir, "town.json")  # 乡镇
village_file_path = os.path.join(lib_dir, "village.json")  # 村镇


def handle_code_file(address_file):
    logger.info(f"read {address_file_path}")
    address = json.load(open(address_file, encoding='utf-8'))
    province_dict = {}
    city_dict = {}
    district_dict = {}
    town_dict = {}
    village_dict = {}
    for province_code in address:
        province_info = address[province_code]
        province_name = province_info["province_name"]
        province_dict[province_code] = province_name  # 获取省份编码映射
        city_datas = province_info["city_datas"]
        for city_code in city_datas:
            city_info = city_datas[city_code]
            city_name = city_info["city_name"]
            city_dict[city_code] = city_name  # 获取城市编码映射
            district_datas = city_info["district_datas"]
            for district_code in district_datas:
                district_info = district_datas[district_code]
                district_name = district_info["district_name"]
                district_dict[district_code] = district_name  # 获取区县编码映射
                town_datas = district_info["town_datas"]
                for town_code in town_datas:
                    town_info = town_datas[town_code]
                    town_name = town_info["town_name"]
                    town_dict[town_code] = town_name  # 获取区县编码映射
                    village_datas = town_info["village_datas"]
                    for village_code in village_datas:
                        village_name = village_datas[village_code]
                        village_dict[village_code] = village_name
        logger.info(f"generate file {province_file_path}")
        with open(province_file_path, 'w', encoding='utf-8') as f:
            f.write(json.dumps(province_dict, indent=2, ensure_ascii=False))

        logger.info(f"generate file {city_file_path}")
        with open(city_file_path, 'w', encoding='utf-8') as f:
            f.write(json.dumps(city_dict, indent=2, ensure_ascii=False))

        logger.info(f"generate file {district_file_path}")
        with open(district_file_path, 'w', encoding='utf-8') as f:
            f.write(json.dumps(district_dict, indent=2, ensure_ascii=False))

        logger.info(f"generate file {town_file_path}")
        with open(town_file_path, 'w', encoding='utf-8') as f:
            f.write(json.dumps(town_dict, indent=2, ensure_ascii=False))

        logger.info(f"generate file {village_file_path}")
        with open(village_file_path, 'w', encoding='utf-8') as f:
            f.write(json.dumps(village_dict, indent=2, ensure_ascii=False))


if __name__ == '__main__':
    handle_code_file(address_file_path)
